package com.nep.controller;

import com.nep.entity.AqiFeedback;
import com.nep.entity.GridMember;
import com.nep.io.FileIO;
import com.nep.service.AqiFeedbackService;
import com.nep.service.impl.AqiFeedbackServiceImpl;
import com.nep.util.JavafxUtil;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.io.File;

public class NepmAqiAssignViewController implements Initializable {
    @FXML
    private TextField txt_afId;
    @FXML
    private Label label_afId;
    @FXML
    private Label label_afName;
    @FXML
    private Label label_proviceName;
    @FXML
    private Label label_cityName;
    @FXML
    private Label label_address;
    @FXML
    private Label label_infomation;
    @FXML
    private Label label_estimateGrade;
    @FXML
    private Label label_date;
    @FXML
    private ComboBox<String> combo_realName;
    
    //当前舞台
    public static Stage aqiInfoStage;
    //多态
    private AqiFeedbackService aqiFeedbackService = new AqiFeedbackServiceImpl();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //初始化标签
        initController();
        
        //初始化网格员
        File file = new File("gridmember.txt");
        if (!file.exists()) {
            JavafxUtil.showAlert(aqiInfoStage, "错误", "网格员数据文件不存在", "请确保gridmember.txt文件存在", "error");
            return;
        }
        
        if (file.length() == 0) {
            JavafxUtil.showAlert(aqiInfoStage, "警告", "网格员数据文件为空", "请先初始化网格员数据", "warn");
            return;
        }
        
        try {
            List<GridMember> glist = (List<GridMember>) FileIO.readObject("gridmember.txt");
            if (glist == null) {
                JavafxUtil.showAlert(aqiInfoStage, "错误", "读取网格员数据失败", "请检查gridmember.txt文件格式", "error");
                return;
            }
            
            boolean hasWorkingMembers = false;
            for (GridMember gm : glist) {
                if (gm != null && gm.getState() != null && gm.getState().equals("工作中")) {
                    combo_realName.getItems().add(gm.getRealName());
                    hasWorkingMembers = true;
                }
            }
            
            if (!hasWorkingMembers) {
                JavafxUtil.showAlert(aqiInfoStage, "警告", "没有工作中的网格员", "请确保有网格员处于工作状态", "warn");
            }
        } catch (Exception e) {
            e.printStackTrace();
            JavafxUtil.showAlert(aqiInfoStage, "错误", "处理网格员数据失败", "请检查gridmember.txt文件格式", "error");
        }
    }

    private void initController() {
        label_afId.setText("");
        label_afName.setText("");
        label_proviceName.setText("");
        label_cityName.setText("");
        label_address.setText("");
        label_infomation.setText("");
        label_estimateGrade.setText("");
        label_date.setText("");
    }

    public void queryFeedback(){
        String afId = txt_afId.getText();
        if (afId == null || afId.trim().isEmpty()) {
            JavafxUtil.showAlert(aqiInfoStage, "查询失败", "请输入反馈数据编号", "请输入要查询的AQI反馈数据编号", "warn");
            return;
        }

        List<AqiFeedback> alist = (List<AqiFeedback>)FileIO.readObject("aqifeedback.txt");
        boolean flag = true;
        if (alist != null) {
            for (AqiFeedback af : alist) {
                if (af != null && af.getAfId() != null && af.getState() != null && 
                    af.getAfId().toString().equals(afId) && af.getState().equals("未指派")) {
                    flag = false;
                    label_afId.setText(af.getAfId()+"");
                    label_afName.setText(af.getAfName() != null ? af.getAfName() : "");
                    label_address.setText(af.getAddress() != null ? af.getAddress() : "");
                    label_cityName.setText(af.getCityName() != null ? af.getCityName() : "");
                    label_date.setText(af.getDate() != null ? af.getDate() : "");
                    label_estimateGrade.setText(af.getEstimateGrade() != null ? af.getEstimateGrade() : "");
                    label_infomation.setText(af.getInfomation() != null ? af.getInfomation() : "");
                    label_proviceName.setText(af.getProviceName() != null ? af.getProviceName() : "");
                    break;
                }
            }
        }
        if(flag){
            JavafxUtil.showAlert(aqiInfoStage, "查询失败", "未找到当前编号反馈信息", "请重新输入AQI反馈数据编号","warn");
            initController();
        }
    }

    public void assignGridMember() {
        String afId = txt_afId.getText();
        if (afId == null || afId.trim().isEmpty()) {
            JavafxUtil.showAlert(aqiInfoStage, "指派失败", "请输入反馈数据编号", "请输入要指派的AQI反馈数据编号", "warn");
            return;
        }

        String gmName = combo_realName.getValue();
        if (gmName == null || gmName.isEmpty()) {
            JavafxUtil.showAlert(aqiInfoStage, "指派失败", "请选择网格员", "请选择要指派的网格员", "warn");
            return;
        }
        
        try {
            aqiFeedbackService.assignGridMember(afId, gmName);
            JavafxUtil.showAlert(aqiInfoStage, "指派成功", "网格员指派成功", "网格员" + gmName + "已成功指派", "info");
            initController();
            txt_afId.setText("");
        } catch (Exception e) {
            JavafxUtil.showAlert(aqiInfoStage, "指派失败", "网格员指派失败", "请检查反馈数据编号是否正确", "warn");
        }
    }
}